﻿using System.Text;
using System.Text.RegularExpressions;
using JetBrains.Annotations;



namespace Crocodile.LangUtils
{
    public static class StringExt
    {

        public static int Offset (this string str)
        {
            if (str != null)
                for (int i = 0,n = str.Length; i < n; i++)
                    if (!char.IsWhiteSpace(str[i]))
                        return i;

            return int.MaxValue;
        }


        public static string InsertSpaces (this string str, int count)
        {
            int n = str.Length;
            var buf = new StringBuilder(n + count);
            for (int i = 0; i < count; i++) buf.Append(' ');
            buf.Append(str);
            return buf.ToString();
        }


        [CanBeNull]
        public static Match Matches([CanBeNull] this string str, [NotNull] Regex pattern)
        {
            if (str == null)
                return null;

            var match = pattern.Match(str);
            return match.Success ? match : null;
        }


        [CanBeNull]
        public static string TruncateRight([CanBeNull] this string str, int count)
        {
            if (str == null)
                return null;

            int n = str.Length;
            return count < n ? str.Substring(0, n - count) : string.Empty;
        }


        [CanBeNull]
        public static string AppendIfNo([CanBeNull] this string str, [NotNull] string suffix)
        {
            return str == null 
                ? null 
                : str.EndsWith(suffix) 
                    ? str 
                    : str + suffix;
        }


        [CanBeNull]
        public static string ToLowerCamel([CanBeNull] this string str)
        {
            if (str == null) return null;
            if (str.Length == 0) return str;

            string c1 = str.Substring(0,1).ToLower();
            string rest = str.Substring(1);
            return c1 + rest;
        }


        [CanBeNull]
        public static string ToUpperCamel([CanBeNull] this string str)
        {
            if (str == null) return null;
            if (str.Length == 0) return str;

            string c1 = str.Substring(0,1).ToUpper();
            string rest = str.Substring(1);
            return c1 + rest;
        }


        [CanBeNull]
        public static string Plural([CanBeNull] this string str)
        {
            if (str == null) return null;
            if (str.Length == 0) return str;

            if (str.EndsWith("s"))
                return str + "es";
            if (str.EndsWith("ex"))
                return str.TruncateRight(2) + "ices";
            if (str.EndsWith("y"))
                return str.TruncateRight(1) + "ies";
            return str + "s";
        }


        [CanBeNull]
        public static string Singular([CanBeNull] this string str)
        {
            if (str == null) return null;
            if (str.Length == 0) return str;

            if (str.EndsWith("ices"))
                return str.TruncateRight(4) + "ex";
            if (str.EndsWith("ies"))
                return str.TruncateRight(3) + "y";
            if (str.EndsWith("s"))
                return str.TruncateRight(1);
            return str;
        }

    }
}
